package org.artifact.core.plugin.datasource;

import cn.hutool.db.ds.DSFactory;
import cn.hutool.setting.Setting;
import org.artifact.core.lang.IPlugin;
import org.artifact.core.lang.IServer;
import org.artifact.core.lang.MapPlus;

import javax.sql.DataSource;
import java.util.Map;

public class DataSourcePlugin implements IPlugin {

	private DSFactory dsFactory = null;
	
	@Override
	public boolean start() {
		Map config = IServer.me().getContext().getConfig();
		Map dataSource = MapPlus.get("plugins.dataSource",config);
		Map druid = (Map) dataSource.remove("druid");
//		Map druid = MapPlus.get("plugins.dataSource.druid",config);

		Setting setting = new Setting();

		dataSource.forEach((k,v)->{
			setting.put(k.toString(),v.toString());
		});

		druid.forEach((k,v)->{
			setting.put(k.toString(),v.toString());
		});


		dsFactory = DSFactory.create(setting);
		return true;
	}

	@Override
	public boolean stop() {
		return true;
	}


	public DataSource getDataSource() {
		return dsFactory.getDataSource();
	}
}
